package com.jy.wms.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsPurchaseOrderDetailedMapper;
import com.jy.wms.dao.WmsQualityTestingDetailedMapper;
import com.jy.wms.pojo.WmsPurchaseOrderDetailed;
import com.jy.wms.pojo.WmsQualityTesting;
import com.jy.wms.dao.WmsQualityTestingMapper;
import com.jy.wms.pojo.WmsQualityTestingDetailed;
import com.jy.wms.pojo.WmsShipmentOrderDetailed;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: WmsQualityTestingService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Fri Nov 03 11:02:52 GMT+08:00 2017
 */

@Service
public class WmsQualityTestingService extends BaseService<WmsQualityTestingMapper, WmsQualityTesting> {
    @Autowired
    private WmsQualityTestingDetailedMapper wmsQualityTestingDetailedMapper;

    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     * @return int
     */
    @Transactional
    public Integer baseInsert(WmsQualityTesting entity) throws SucessException, ServiceException {
        //明细集合
        List<WmsQualityTestingDetailed> list = entity.getDetailedList();
        //查询数据库是否有相同名称的数据
        Integer a = dao.getCountByMc(entity.getMc(), entity.getSshzid());
        if (a <= 0) {
            //质检方式”为“全检”不需要添加规则明细,质检方式”为“百分比抽检”或“固定数量抽检”则需要添加规则明细
            //“新增规则明细”、“编辑规则明细”、“删除规则明细”、“查看规则明细”可用，明细不能为空
            Integer b = 0;
            Integer c = 0;
            if (!"0".equals(entity.getFs()) && list == null) {
                throw new ServiceException("质检规则明细不可为空");
            } else if ("0".equals(entity.getFs())) {
                b = dao.baseInsert(entity);
                if (b > 0) {
                    throw new SucessException("质检规则增加成功");
                } else {
                    throw new ServiceException("质检规则增加失败");
                }
            } else {
                for (WmsQualityTestingDetailed wqtd : list) {
                    wqtd.setId(UUIDUtils.uuid());
                    wqtd.setSszjgzid(entity.getId());
                    wqtd.setCkid(entity.getCkid());
                    if("1".equals(entity.getFs())) {//百分比抽检
                        wqtd.setBfb(String.valueOf(Double.parseDouble(wqtd.getBfb()) / 100));
                    }
                }
                b = dao.baseInsert(entity);
                if (b > 0) {
                    c = wmsQualityTestingDetailedMapper.baseInsert(list);
                }
                if (c > 0) {
                    throw new SucessException("质检规则增加成功");
                } else {
                    throw new ServiceException("质检规则增加失败");
                }
            }
        } else {
            throw new ServiceException("质检规则名称重复，增加失败");
        }
    }

    /**
     * 插入一条记录（匹配有值的字段）
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsertSelective(WmsQualityTesting entity) {
        return dao.baseInsertSelective(entity);
    }

    /**
     * 删除（根据主键ID删除）
     *
     * @param id 主键
     * @return int
     */
    public Integer baseDeleteByPrimaryKey(String id) {
        return dao.baseDeleteByPrimaryKey(id);
    }

    /**
     * 删除（根据ID 批量删除）
     *
     * @param idList 主键ID列表
     * @return int
     */
    public Integer baseDeleteBathPrimaryKeys(List<String> idList) {
        return dao.baseDeleteBathPrimaryKeys(idList);
    }

    /**
     * 分页查询
     *
     * @param entity 实体
     * @return Page<WmsQualityTesting>
     */
    public Page<WmsQualityTesting> baseSelectPage(WmsQualityTesting entity, String hzid,String hzmc) {
        PageHelper.startPage(entity.getPage(), entity.getRows());
        Page<WmsQualityTesting> list= (Page<WmsQualityTesting>) dao.baseSelectPage(entity, hzid,hzmc);
        return list;
    }

    /**
     * 查询（根据主键ID查询）
     *
     * @param id 主键ID
     * @return WmsQualityTesting
     */
    public WmsQualityTesting baseSelectByPrimaryKey(String id) {
        return dao.baseSelectByPrimaryKey(id);
    }

    /**
     * 查询（根据ID 批量查询）
     *
     * @param idList 主键ID列表
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys(List<WmsQualityTesting> idList) {
        return dao.baseSelectBathPrimaryKeys(idList);
    }

    /**
     * 修改 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKeySelective(WmsQualityTesting entity, String hzid) throws ServiceException, SucessException {
        //删除数据库里原规则下的所有明细
        //通过id得到对象
        WmsQualityTesting wmsQualityTesting = dao.baseSelectByPrimaryKey(entity.getId());
        //如果当前id在数据库的对象的名称与现在修改的名称一致，此时不需要数据库查重了，也不需要更改规则行了，直接更改明细即可；
        //如果当前id在数据库的对象的名称与现在修改的名称不一致，那么需要查询当前修改的这个名称在数据库中是否有相同的数据
        if (!wmsQualityTesting.getMc().equals(entity.getMc())) {
            //查询数据库是否有相同名称的数据
            Integer a = dao.getCountByMc(entity.getMc(), hzid);
            if (a <= 0) {
                //修改主规则
                Integer b = dao.baseUpdateByPrimaryKeySelective(entity);
                if(b>0){
                    throw new SucessException("质检规则修改成功");
                }else{
                    throw new ServiceException("质检规则修改失败");
                }
            }else{
                throw new ServiceException("质检规则名称重复，增加失败");
            }
        } else{
            Integer b = dao.baseUpdateByPrimaryKeySelective(entity);
            if(b>0){
                throw new SucessException("质检规则修改成功");
            }else{
                throw new ServiceException("质检规则修改失败");
            }
        }
    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey(WmsQualityTesting entity) {

        return dao.baseUpdateByPrimaryKey(entity);
    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param mc 名称模糊查询
     * @param ckid 仓库ID
     * @param sshzid 所属货主ID
     * @return int
     */
    public List<WmsQualityTesting> selectZjgzBymc(String mc,String ckid,String sshzid){
        return dao.selectZjgzBymc(mc,ckid,sshzid);
    }





}
